var common = require('../../utils/common.js');
const app = getApp()
Page({
  data: {
    classification: [],
    current: 0,
    items: [],
    onlyWx: false,
    itemloading: true,
    itemIsShow: false,
    page: 1,
    categoryId: '',
    userInfo: app.globalData.userInfo,
    tipshow: true,
    more: true,
    rToolsShow: false,
    nineloading: true,
    currentTab: 0,
    todayitems:[],
    spItem:[],
    seachitems:[],
    itemloading: true,
    todayloading: true,
    nineloading: true,
    seachloading: true,
    from: '',
    sppage: 1,
    sploading: true,
    path: '',
    type: '',
    tabNav:[
      {
        title: '高佣爆品',
        path: '/app/v2/goods/specialGoods',
        type: 140
      },
      {
        title: '大家都在推',
        path: '/app/v2/goods/specialGoods',
        type: 150
      },
      {
        title: '推手精选',
        path: '/app/v2/goods/specialGoods',
        type: 23
      },
    ]
  },
  onLoad: function (options) {
    console.log(options)
    let num = options.index
    let item = this.data.tabNav[num]
    this.setData({
      currentTab: num
    })
    wx.setNavigationBarTitle({
      title: '热推商品'
    })
    this.getSpItem(item.path,item.type)
    console.log(app.globalData.userInfo)
    this.setData({
      userInfo: app.globalData.userInfo
    })
  },
  swichNav(e){
    console.log(e)
    this.setData({
      currentTab: e.currentTarget.dataset.index,
      path: e.currentTarget.dataset.path,
      type: e.currentTarget.dataset.type
    })
    this.getSpItem(this.data.path,this.data.type)
  },
  getSpItem(path,type){
    const that = this
    this.setData({
      spItem: [],
      sploading: true
    });
    common.post({
      url: path, data: { type: type,pageNo: 1,sessionId:app.globalData.sessionId }
    })
      .then((res) => {
        console.log(res.data)
        that.setData({
          spItem: res.data.data,
        });
        if(res.data.total == 0){
          that.setData({
            noSpItem: true,
            sploading: false
          })
        }else if(res.data.total < 11){
          that.setData({
            sploading: false,
          });
        }
      }).catch((res) => {
        console.log(res)
      })
  },
  getSpItempage(path,type) {
    const that = this;
    var page = that.data.sppage + 1;
    common.post({ url: path, data: { type: type, pageNo: page,sessionId:app.globalData.sessionId } })
      .then((res) => {
        var data = that.data.spItem;
        console.log(res.data)
        if (that.data.sppage * 10 < res.data.total) {
          that.setData({
            sppage: page
          });
          var postList = res.data.data;
          for (let i = 0; i < postList.length; i++) {
            let tmp = postList[i];
            data.push(tmp);
            setTimeout(function () {
              that.setData({
                spItem: data
              });
              console.log(data)
            }, 300)
          }
        } else {
          console.log('没有了')
          wx.showToast({
            title: '没有了',
            icon: 'none',
            duration: 2000
          })
          that.setData({
            sploading: false,
          });
        }

      }).catch((res) => {
        console.log(res)

      })
  },
  onReachBottom() {
    console.log('bottom')
    this.getSpItempage(this.data.path,this.data.type)
  },
  buybtn(e) {
    wx.navigateTo({
      url: '/pages/itemdetailNew/itemdetailNew?sku=' + e.currentTarget.dataset.item.skuId
    })
  },
  closeTip(){
    this.setData({
      tipshow: false
    })
  },
  totop(){
    this.setData({
      scrollTop: 0
    })
  },
  seachlink: function () {
    wx.navigateTo({
      url: '/pages/seach/seach'
    })
  },

})